#coding=utf8
import time,datetime
#import itchat
from binancetrade import binance

line='-'*25+'\n'
wxid='filehelper'


def ma_price_rate(symbol_klines,x,n=0):
    ba=binance()
    symbol=symbol_klines[0]
    klines=symbol_klines[1]
    ma_x=ba.get_MA(x,n,symbol_klines,1)
    if n==0:
        close_pri=ba.get_symbol_price(symbol)
    else:
        close_pri=float(klines[-n][4])
    rate=(ma_x-close_pri)/ma_x
    return rate

def max_down_speed(klines):#最大跌速，通过计算现价与ma(99)的差值
    max_rate=0
    for r in range(1,len(klines[1])-99):
        rate=ma_price_rate(klines,99,r)
        if rate>max_rate:
            max_rate=rate
    return max_rate



def main():
    info='-'*25+'\n'

    info+='【相对于MA(99)线跌幅】\n'
    print(info)
    interval_dict={'1m':'1分钟','5m':'5分钟','15m':'15分钟'}
    interval='5m'#K线类型
    limit=500    #数据量，最大500
    #itchat.send(info, wxid)#发微信消息

    ba=binance()
    for sym in ba.get_symbol_list('BTC'):
        klines=ba.get_Klines(sym,interval,limit)
        now=ma_price_rate(klines,99,n=0)
        max=max_down_speed(klines)
        qut=ba.get_ticker_24hr_quoteVolume(sym)
        if qut>200 and now>0.02:
            info='[%s]24小时成交额：%.2fBTC   最大跌幅:%.2f%%，现跌幅:%.2f%%'%(sym,qut,max*100,now*100)
            print(info)

if __name__=='__main__':
    main()
